package com.ws.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.ws.bean.Vehicle;
import com.ws.bean.VehicoleBrand;
import com.ws.common.CommonResult;
import com.ws.service.VehicleService;
import com.ws.util.Page;

@RestController
public class VehicleController {
	@Autowired
	VehicleService  service;
	
	//查询所有车辆
	@GetMapping("select")
	public CommonResult getAllVehicles(Vehicle vehicle , Page page) {
		List<Vehicle> list = service.getAllVehicles(vehicle , page);
		int count = service.getCount(vehicle);
		Map map = new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		return CommonResult.success(map);
	}
	@GetMapping("getbrand")
	public CommonResult getAllBrands() {
		List<VehicoleBrand> list = service.getAllBrands();
		return CommonResult.success(list);
	}
	//增加车辆
	@PostMapping("add")
	public CommonResult addVehicle(@RequestBody Vehicle vehicle) {
		int result = service.addVehicle(vehicle);
		if(result > 0) return CommonResult.success(result);
		else return CommonResult.fail();
	}
	//删除车俩
	@DeleteMapping("del/{vehicleid}")
	public CommonResult delVehicle(@PathVariable Integer vehicleid) {
		int result = service.delVehicle(vehicleid);
		if(result > 0 ) return  CommonResult.success(result);
		else return  CommonResult.fail();
	}
	//修改车辆信息
	@PutMapping("edit")
	public CommonResult editVehicle( Vehicle vehicle) {
		int result = service.editVehicle(vehicle);
		return CommonResult.success();
	}
}
